  <? if(!empty($student_grades)) :?>

<table class="table table-bordered">
		<tr class="info">
			<td>Subject Code</td>
			<? if(!empty($grading_periods)) :?>
			<? foreach($grading_periods as $row) :?>
			<td style="text-align:center;"><?= ucfirst(strtolower($row->gp_desc)) ;?></td>
			<? endforeach;?>

			<?if(isset($student_profile) && strtolower($student_profile->level_code) !== "pre-kinder"):?>
				<td style="text-align:center;">Final Grade</td>
			<?endif;?>

			<? endif ;?>
		</tr>
		<tbody class="tabular">
	<? foreach($student_grades as $row) :?>
  		<?php
  			/** COMPUTE FOR FINAL GRADE ONLY WHEN ALL GRADING PERIOD EXIST OR HAS GRADE **/
  			$final_grade = 0;
  			$grade_total = 0;
  			$gp_ctr = 0;
  			$gp_total = count($grading_periods);
  		?>
			<tr>
				<td style="background:#c0c0c0;font-weight:bold;"><?= $row->subj_code ;?></td>
				<? foreach($grades as $r) :?>
				<? if($r->subject_id == $row->subj_id) :?>
					<?php
						if($r->value){
							$gp_ctr++;
							$grade_total += floatval($r->value);
						}
					?>
					<td style="text-align:center;">
						<?if(isset($student_profile) && strtolower($student_profile->level_code) !== "pre-kinder"):?>
							<div class="btn-group">
							  <button type="button" class="btn btn-mini btn-primary"><?=_convert($r->value)?></button>
							  <button type="button" class="btn btn-mini btn-success"><?=$r->value?></button>
							</div>
						<?else:?>
							<?=_convert($r->value)?>
						<?endif;?>
					</td>
				<? endif ;?>
				<? endforeach ;?>
				
				<?php
					if($gp_total === $gp_ctr){
						$final_grade = round($grade_total / $gp_ctr, 3);
					}
					// vp($student_profile);
				?>
				
				<?if(isset($student_profile) && strtolower($student_profile->level_code) !== "pre-kinder" && $final_grade):?>
					<td>
						<div class="btn-group">
						  <button type="button" class="btn btn-mini btn-primary"><?=_convert($final_grade)?></button>
						  <button type="button" class="btn btn-mini btn-success"><?=$final_grade?></button>
						</div>
					</td>
				<?endif;?>

			</tr>
	<? endforeach ;?>
	</tbody>
</table>
  <? else: ?>
		<code>
			Unable to get student Grades. Because 1) Student is not assigned to a section, 2) student is not allowed to view grades and remarks.
		</code>
  <? endif;?>

<div><a href="#" data-reveal-id="gradeTable" class="btn btn-mini">show grading table</a></div>